<template>
	<view class="container">
		<!-- 搜索栏 -->
		<view class="search-box">
			<view class="search-input" @click="goSearch">
				<text class="label">搜索:</text>
				<text class="placeholder">搜索商品</text>
			</view>
		</view>

		<!-- 轮播图 -->
		<swiper class="banner" circular autoplay interval="3000" duration="1000">
			<swiper-item v-for="(item, index) in bannerList" :key="index" @click="tapBanner(item)">
				<image :src="item.image" mode="aspectFill"></image>
			</swiper-item>
		</swiper>

		<!-- 分类导航 -->
		<view class="nav-section">
			<view class="section-header">
				<text class="title">商品分类</text>
			</view>
			<view class="nav-items">
				<view class="nav-item" v-for="(item, index) in categoryList" :key="index" @click="tapCategory(item)">
					<image :src="item.icon" mode="aspectFit"></image>
					<text>{{ item.name }}</text>
				</view>
			</view>
		</view>

		<!-- 推荐商品 -->
		<view class="recommend-section">
			<view class="section-header">
				<text class="title">精选推荐</text>
				<text class="more" @click="moreRecommend">查看更多</text>
			</view>
			<view class="product-list">
				<view class="product-item" v-for="(item, index) in recommendList" :key="index" @click="goDetail(item)">
					<view class="image-wrapper">
						<image :src="item.image" mode="aspectFill"></image>
					</view>
					<view class="info">
						<text class="name">{{ item.name }}</text>
						<text class="price">¥{{ item.price }}</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 热卖商品 -->
		<view class="hot-section">
			<view class="section-header">
				<text class="title">热卖爆品</text>
				<text class="more" @click="moreHot">查看更多</text>
			</view>
			<view class="product-list">
				<view class="product-item" v-for="(item, index) in hotList" :key="index" @click="goDetail(item)">
					<view class="image-wrapper">
						<image :src="item.image" mode="aspectFill"></image>
					</view>
					<view class="info">
						<text class="name">{{ item.name }}</text>
						<text class="price">¥{{ item.price }}</text>
						<text class="hot-tag">热卖</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerList: [{
					id: 1,
					image: '/static/banner/banner1.jpg',
					url: ''
				}, {
					id: 2,
					image: '/static/banner/banner2.jpg',
					url: ''
				}],
				categoryList: [{
					id: 1,
					name: '新鲜水果',
					icon: '/static/category/fruit.png'
				}, {
					id: 2,
					name: '蔬菜豆制',
					icon: '/static/category/vegetable.png'
				}, {
					id: 3,
					name: '肉禽蛋品',
					icon: '/static/category/meat.png'
				}, {
					id: 4,
					name: '海鲜水产',
					icon: '/static/category/seafood.png'
				}, {
					id: 5,
					name: '乳品烘焙',
					icon: '/static/category/milk.png'
				}],
				recommendList: [],
				hotList: []
			}
		},
		onLoad() {
			this.getRecommendList();
			this.getHotList();
		},
		onPullDownRefresh() {
			this.getRecommendList();
			this.getHotList();
		},
		methods: {
			goSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			tapBanner(item) {
				if (item.url) {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			tapCategory(item) {
				uni.navigateTo({
					url: `/pages/product/list?category=${item.id}&name=${item.name}`
				})
			},
			moreRecommend() {
				uni.navigateTo({
					url: '/pages/product/recommend'
				})
			},
			moreHot() {
				uni.navigateTo({
					url: '/pages/product/hot'
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: `/pages/product/detail?id=${item.id}`
				})
			},
			getRecommendList() {
				// 恢复为本地静态数据
				this.recommendList = [
					{
						id: 1,
						name: '新鲜草莓',
						price: '38.8',
						image: '/static/product/strawberry.jpg'
					},
					{
						id: 2,
						name: '有机生菜',
						price: '12.8',
						image: '/static/product/lettuce.jpg'
					},
					{
						id: 3,
						name: '精选牛肉',
						price: '88.8',
						image: '/static/product/beef.jpg'
					},
					{
						id: 4,
						name: '深海鲜虾',
						price: '68.8',
						image: '/static/product/shrimp.jpg'
					}
				];
				uni.stopPullDownRefresh();
			},
			getHotList() {
				// 恢复为本地静态数据
				this.hotList = [
					{
						id: 5,
						name: '有机番茄',
						price: '15.8',
						image: '/static/product/tomato.jpg'
					},
					{
						id: 6,
						name: '新鲜鸡蛋',
						price: '29.9',
						image: '/static/product/eggs.jpg'
					},
					{
						id: 7,
						name: '纯牛奶',
						price: '59.9',
						image: '/static/product/milk.jpg'
					},
					{
						id: 8,
						name: '有机黄瓜',
						price: '8.8',
						image: '/static/product/cucumber.jpg'
					}
				];
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 30rpx;
		background: #f6f6f6;
	}

	.search-box {
		padding: 20rpx 30rpx;
		background-color: #fff;
		position: sticky;
		top: 0;
		z-index: 100;
		
		.search-input {
			display: flex;
			align-items: center;
			height: 70rpx;
			padding: 0 30rpx;
			background: #f5f5f5;
			border-radius: 35rpx;
			box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
			
			.label {
				font-size: 30rpx;
				color: #999;
				margin-right: 10rpx;
			}
			
			.placeholder {
				font-size: 28rpx;
				color: #999;
			}
		}
	}

	.banner {
		width: 100%;
		height: 320rpx;
		margin-bottom: 20rpx;
		
		image {
			width: 100%;
			height: 100%;
		}
	}

	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0;
		margin-bottom: 10rpx;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			position: relative;
			padding-left: 16rpx;
			
			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 6rpx;
				height: 30rpx;
				width: 6rpx;
				background: #FF5151;
				border-radius: 3rpx;
			}
		}
		
		.more {
			font-size: 26rpx;
			color: #999;
		}
	}

	.nav-section {
		margin: 0 20rpx 20rpx;
		background: #fff;
		padding: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
		
		.nav-items {
			display: flex;
			flex-wrap: wrap;
		}
		
		.nav-item {
			width: 20%;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 20rpx 0;
			
			image {
				width: 90rpx;
				height: 90rpx;
				margin-bottom: 12rpx;
				border-radius: 45rpx;
				box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
			}
			
			text {
				font-size: 24rpx;
				color: #333;
				text-align: center;
			}
		}
	}

	.recommend-section, .hot-section {
		margin: 0 20rpx 20rpx;
		background: #fff;
		padding: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
	}

	.product-list {
		display: flex;
		flex-wrap: wrap;
		margin: 0 -10rpx;
		
		.product-item {
			width: calc(50% - 20rpx);
			margin: 10rpx;
			background: #fff;
			border-radius: 12rpx;
			overflow: hidden;
			box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
			
			.image-wrapper {
				width: 100%;
				position: relative;
				padding-top: 75%; // 4:3比例（可改为100%为1:1）
				overflow: hidden;
				background: #f5f5f5;
			}
			
			image {
				position: absolute;
				left: 0; top: 0;
				width: 100%;
				height: 100%;
				object-fit: cover; // 保证图片不变形
				border-radius: 12rpx 12rpx 0 0;
			}
			
			.info {
				padding: 16rpx;
				position: relative;
				
				.name {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 10rpx;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
					overflow: hidden;
				}
				
				.price {
					font-size: 30rpx;
					color: #FF5151;
					font-weight: bold;
				}
				
				.hot-tag {
					position: absolute;
					top: 16rpx;
					right: 16rpx;
					background: #FF5151;
					color: #fff;
					font-size: 20rpx;
					padding: 2rpx 8rpx;
					border-radius: 4rpx;
				}
			}
		}
	}
</style>
